<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 1、打印一个参数值内能被3或5或7整除的数
        // 2、打印斐波那契数列的第N位
        // 3、打印从0到一个数的累加器
        var initProgrammer = (function(){
            function judge(n){
                if(isNaN(n)){
                    return false;
                }
                if(n <= 0){
                    return false;
                }
                return true;
            }

            // 1、打印一个参数值内能被3或5或7整除的数
            function divisible(n){
                if(judge(n) === false) return;
                var arr = [];
                for(var i = 1; i <= n; i++){
                    if(i % 3 === 0 || i % 5 === 0 || i % 7 === 0){
                        arr.push(i);
                    }
                }
                return arr;
            }
            // 打印斐波那契数列的第N位(方法1)
            function fb1(n){
                if(judge(n) === false) return;

                if(n == 1 || n == 2){
                    return 1;
                }
                return fb1(n - 1) + fb1(n - 2);
            }
            // 打印斐波那契数列的第N位(方法2)
            function fb2(n){
                if(judge(n) === false) return;

                var n1 = 1,
                    n2 = 1,
                    num = 0;
                if(n == 1 || n == 2){
                    num = 1
                    return num;
                }
                for(var i = 2; i < n; i++){
                    num = n1 + n2;
                    n1 = n2;
                    n2 = num;
                }
                return num;
            }

            // 累加1
            function sum1(n){
                if(judge(n) === false) return;

                var num = 0;
                for(var i = 1; i <= n; i++){
                    num += i;
                }
                return num;
            }

            // 累加2
            function sum2(n){
                if(judge(n) === false) return;
                
                if(n == 1) {
                    return 1;
                }

                return n + sum2(n - 1);
            }

            return {
                sum1,
                sum2,
                fb1,
                fb2,
                divisible
            }
        })();
        console.log(initProgrammer.fb1(1));
        console.log(initProgrammer.fb2(2));
        console.log(initProgrammer.divisible(10));
        console.log(initProgrammer.sum1(3));
        console.log(initProgrammer.sum2(4));
    </script>
</body>
</html>